#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import os

from onvif import ONVIFCamera  # noqa

from onvif_ptz.path_conf import BASE_DIR

with open(os.path.join(BASE_DIR, 'camera_conf.json'), encoding='utf-8', mode='r') as fp:
    camera_data = json.load(fp)


class Onvif:

    @property
    def onvif_init(self):
        try:
            oc = ONVIFCamera(
                host=camera_data['ip'],
                port=camera_data['port'],
                user=camera_data['username'],
                passwd=camera_data['password']
            )
        except Exception as e:
            raise RuntimeError(f'初始化相机 ONVIF 失败: {e}')
        return oc

    @property
    def media_service(self):
        return self.onvif_init.create_media_service()

    @property
    def ptz_service(self):
        return self.onvif_init.create_ptz_service()
